package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.iot.domain.bo.IotElevatorFaultBo;
import org.dromara.iot.domain.bo.IotElevatorRepairBo;
import org.dromara.iot.domain.bo.IotFaultRecordBo;
import org.dromara.iot.domain.vo.IotElevatorRepairVo;
import org.dromara.iot.service.IIotElevatorRepairService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电梯维修
 *
 * @author lsl
 * @date 2024-06-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/elevatorRepair")
public class IotElevatorRepairController extends BaseController {

    private final IIotElevatorRepairService iotElevatorRepairService;

    /**
     * 查询电梯维修列表
     */
    @SaCheckPermission("iot:elevatorRepair:list")
    @GetMapping("/list")
    public TableDataInfo<IotElevatorRepairVo> list(IotElevatorRepairBo bo, PageQuery pageQuery) {
        return iotElevatorRepairService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯维修列表
     */
    @SaCheckPermission("iot:elevatorRepair:export")
    @Log(title = "电梯维修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotElevatorRepairBo bo, HttpServletResponse response) {
        List<IotElevatorRepairVo> list = iotElevatorRepairService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯维修", IotElevatorRepairVo.class, response);
    }

    /**
     * 获取电梯维修详细信息
     *
     * @param repairId 主键
     */
    @SaCheckPermission("iot:elevatorRepair:query")
    @GetMapping("/{repairId}")
    public R<IotElevatorRepairVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long repairId) {
        return R.ok(iotElevatorRepairService.queryById(repairId));
    }

    /**
     * 新增电梯维修
     */
    @SaCheckPermission("iot:elevatorRepair:add")
    @Log(title = "电梯维修", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotElevatorRepairBo bo) {
        return toAjax(iotElevatorRepairService.insertByBo(bo));
    }

    /**
     * 修改电梯维修
     */
    @SaCheckPermission("iot:elevatorRepair:edit")
    @Log(title = "电梯维修", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotElevatorRepairBo bo) {
        return toAjax(iotElevatorRepairService.updateByBo(bo));
    }

    /**
     * 删除电梯维修
     *
     * @param repairIds 主键串
     */
    @SaCheckPermission("iot:elevatorRepair:remove")
    @Log(title = "电梯维修", businessType = BusinessType.DELETE)
    @DeleteMapping("/{repairIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] repairIds) {
        return toAjax(iotElevatorRepairService.deleteWithValidByIds(List.of(repairIds), true));
    }

    /**
     * 接受维修任务接口
     *
     * @param bo
     */
    @Log(title = "电梯维修", businessType = BusinessType.UPDATE)
    @PutMapping("/startRepair")
    public R<Void> startRepair(@RequestBody IotElevatorRepairBo bo) {
        return toAjax(iotElevatorRepairService.startRepair(bo));
    }


    /**
     * 上报电梯故障
     * @TODO 告警！！！！
     */
    @Log(title = "电梯故障", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/report")
    public R<Void> reportFault(@RequestBody IotElevatorFaultBo bo) {
        return toAjax(iotElevatorRepairService.reportFault(bo));
    }

    /**
     * 开始维修工作
     *
     * @param faultRecord 故障记录
     * @return 结果
     */
    @PutMapping("/marchElevatorRepair")
    @RepeatSubmit()
    public R<Void> marchElevatorRepair(@RequestBody IotFaultRecordBo faultRecord) {
        return toAjax(iotElevatorRepairService.marchElevatorRepair(faultRecord));
    }


    /**
     * 完成维修/维修审核评价提交接口
     *
     * @param elevatorRepair 维修信息
     * @return 结果
     */
    @PutMapping("/submitElevatorRepair")
    @RepeatSubmit()
    public R<Void> submitElevatorRepair(@RequestBody IotElevatorRepairBo elevatorRepair) {
        return toAjax(iotElevatorRepairService.submitElevatorRepair(elevatorRepair));
    }

}
